#include "WidgetUIInfo.hpp"


WidgetUIInfo::WidgetUIInfo(const char *pRootPath, const char *pIniUIFilename, const char *pIniDriverMappingFilename, const char *pIniVehiculeClassMappingFilename)
{
	initialized = false;
	rootPath = pRootPath; 

	nbProfiles = GetPrivateProfileInt("Profiles", "count", 0, pIniUIFilename);
	for (int i = 0 ; i < nbProfiles; i++) {
		sprintf(tmp, "Profile.%d", (i+1));
		profiles[i].widgetOffsetX = (signed int)GetPrivateProfileInt(tmp, "UIInfo.offsetX", 0, pIniUIFilename);
		profiles[i].widgetOffsetY = (signed int)GetPrivateProfileInt(tmp, "UIInfo.offsetY", 0, pIniUIFilename);
		profiles[i].hide = GetPrivateProfileInt(tmp, "UIInfo.hide", 0, pIniUIFilename) != 0;
	}

	//std::string str = rootPath;
	GetPrivateProfileString("UI Widgets", "UIInfo.ini", "", tmp, 999, pIniUIFilename);
	//str.append(tmp);
	sprintf(iniUIFilename, "%s%s", rootPath, tmp);
	//iniUIFilename = str.c_str(); //pIniUIFilename; 
	available = GetPrivateProfileInt("UI Info", "available", 0, iniUIFilename) != 0;

}

void WidgetUIInfo::init(const ScreenInfoV01 &info, float ratio, float ratioWidth, float ratioHeight, Sprite spriteList[]) {
	if (!available) {
		initialized = true;
		return;
	}

	for (int i = 0 ; i < nbProfiles; i++) {
		profiles[i].widgetOffsetX *= ratioWidth;
		profiles[i].widgetOffsetY *= ratioHeight;
	}

	int posX = (int)(ratioWidth * GetPrivateProfileInt("UI Info", "image.left", 40, iniUIFilename));//40;
	int posY = (int)(ratioHeight * GetPrivateProfileInt("UI Info", "image.top", 550, iniUIFilename));//550;

	//LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	

	bgImageData.spriteIndex = GetPrivateProfileInt("UI Info", "image.id", 0, iniUIFilename) -1;
	if (bgImageData.spriteIndex > -1) {
		bgImageData.left = posX;
		bgImageData.top = posY;
		bgImageData.width = (int)(GetPrivateProfileInt("UI Info", "image.width", (int)spriteList[bgImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
		bgImageData.height = (int)(GetPrivateProfileInt("UI Info", "image.height", (int)spriteList[bgImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		bgImageData.colorIndex = GetPrivateProfileInt("UI Info", "image.color.id", 1, iniUIFilename) -1;
	}

	versionAvailable = GetPrivateProfileInt("UI Info.Version", "available", 0, iniUIFilename) != 0;
	if (versionAvailable) {
		GetPrivateProfileString("UI Info.Version", "version", "", versionStr, 49, iniUIFilename);
		versionTextData.fontIndex = GetPrivateProfileInt("UI Info.Version", "text.font.id", 1, iniUIFilename) - 1;
		versionTextData.colorIndex = GetPrivateProfileInt("UI Info.Version", "text.font.color.id", 1, iniUIFilename) - 1;
		versionTextData.rect.left = (int)(posX + GetPrivateProfileInt("UI Info.Version", "text.left", 0, iniUIFilename) * ratioWidth);
		versionTextData.rect.top = (int)(posY + GetPrivateProfileInt("UI Info.Version", "text.top", 0, iniUIFilename) * ratioWidth);
		versionTextData.rect.right = (int)(posX + GetPrivateProfileInt("UI Info.Version", "text.right", 100, iniUIFilename) * ratioWidth);
		versionTextData.rect.bottom = (int)(posY + GetPrivateProfileInt("UI Info.Version", "text.bottom", 100, iniUIFilename) * ratioWidth);
		versionTextData.alignment = GetPrivateProfileInt("UI Info.Version", "text.alignment", 1, iniUIFilename);
	}
}
void WidgetUIInfo::render(int profileIndex, const ScreenInfoV01 &info, const LPD3DXFONT *fontList, const D3DCOLOR *colorList, Sprite spriteList[]) { 
	if (!available) return;
		if (gReplayIsActive) return;
	if (profiles[profileIndex].hide) return;

	LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	if (bgImageData.spriteIndex > -1) {
		spriteList[bgImageData.spriteIndex].setPosition(bgImageData.left + profiles[profileIndex].widgetOffsetX, bgImageData.top + profiles[profileIndex].widgetOffsetY);
		spriteList[bgImageData.spriteIndex].setSize(bgImageData.width, bgImageData.height);
		spriteList[bgImageData.spriteIndex].render(dev, colorList[bgImageData.colorIndex], 100);
	}
	if (versionAvailable) {
		tmpRect.left = versionTextData.rect.left + profiles[profileIndex].widgetOffsetX;
		tmpRect.top = versionTextData.rect.top + profiles[profileIndex].widgetOffsetY;
		tmpRect.right = versionTextData.rect.right + profiles[profileIndex].widgetOffsetX;
		tmpRect.bottom = versionTextData.rect.bottom + profiles[profileIndex].widgetOffsetY;
		fontList[versionTextData.fontIndex]->DrawTextA(NULL, versionStr, -1, &tmpRect, versionTextData.alignment, colorList[versionTextData.colorIndex]);
	}

}
WidgetUIInfo::~WidgetUIInfo(void)
{
}
